﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utility;
using Utility.Model;
using BaseUI;
using System.Web.Security;

namespace MsgCenter.Web.Handlers
{
    /// <summary>
    /// Handler_Login 的摘要说明
    /// </summary>
    public class Handler_Login : BaseUI.BaseHandler_Client
    {
        /// <summary>
        /// 获取Cookie登陆名
        /// </summary>
        public void GetCookieValue()
        {
            string strCookieName = ParamsOfRequest.Request("CookieName");
            if (strCookieName == "")
            {
                strCookieName = "UserNameValue";
            }
            string strName = ConvertHelper.GetCookieValue(base.Context, strCookieName);
            base.Response.Write(strName);
        }
        /// <summary>
        /// 登陆
        /// </summary>
        public void Login()
        {
            string strRetMessage = string.Empty;
            string strUserName = ParamsOfRequest.Request("UserName");
            string strPassWord = ParamsOfRequest.Request("PassWord");
            string strRemember = ParamsOfRequest.Request("Remember");
            if (strUserName == string.Empty || strPassWord == string.Empty)
            {
                base.GetFailureJson("出现异常,用户名密码不能为空");
            }
            else
            {
                strUserName = strUserName.ToLower();
                Sys_User model = BLL.BLLServiceHelper.GetUserService().GetLogin(strUserName);
                if (model != null)
                {
                    if (model.Password != strPassWord.ToSafeEncrypt())
                    {
                        base.GetFailureJson("密码输入错误!");
                    }
                    else
                    {
                        WriteCookie(strUserName, strRemember);
                        UserSession mdlUserSession = UserSessionService.GetUserSession(strUserName, base.IP);
                        UserSessionService.SetUserInfo(strUserName, mdlUserSession);
                        FormsAuthentication.SetAuthCookie(strUserName, false);
                        base.GetSuccessJson();
                    }
                }
                else
                {
                    base.GetFailureJson("用户名输入错误!");
                }
            }
        } 
        /// <summary>
        /// 是否写入用户名到Cookie
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <param name="strRemember">true或false</param>
        private void WriteCookie(string strUserName, string strRemember)
        {
            try
            {
                if (strRemember.Length > 0)
                {
                    bool blRemember = false;
                    bool.TryParse(strRemember, out blRemember);
                    if (blRemember)
                    {
                        ConvertHelper.WriteCookie(base.Context, "UserNameValue", strUserName);
                    }
                    //else
                    //{
                    //    ConvertHelper.WriteCookie(base.Context, "UserNameValue", string.Empty);
                    //}
                }
            }
            catch
            { }
        }
    }
}